//
//  ChangeTimerController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/30.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ChangeTimerController.h"

@interface ChangeTimerController ()

/**bgView*/
@property (nonatomic,strong)UIView * bgView;
/*时间选择器*/
@property(nonatomic,strong)UIDatePicker * datePicker;
/**<#属性#>*/
@property (nonatomic,copy)NSString * showStr;
@end

@implementation ChangeTimerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor clearColor];
    [self creatDateChoose];
}
#pragma mark 时间选择器
-(void)creatDateChoose{
    
    self.bgView=[UIView new];
    self.bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.bgView];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(260);
    }];
    
    UIButton * cleanBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cleanBtn setTitle:@"清除" forState:UIControlStateNormal];
    [cleanBtn addTarget:self action:@selector(cleanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:cleanBtn];
    
    UIButton * sureBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:sureBtn];
    
    [cleanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(2);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(2);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    [self.bgView addSubview:self.datePicker];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(cleanBtn.mas_bottom).offset(0);
        
    }];
    
    NSLocale * locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];;
    self.datePicker.locale=locale;
    
    NSDate * localDate=[NSDate date];
    // NSGregorianCalendar 8.0之前 NSCalendarIdentifierGregorian8.0之后
    NSCalendar * greg=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents * offsetComponents=[[NSDateComponents alloc]init];
    
    [offsetComponents setYear:20];
    [offsetComponents setMonth:12];
    [offsetComponents setDay:30];
    NSDate * maxDate=[greg dateByAddingComponents:offsetComponents toDate:localDate options:0];
    NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"YYYY-MM-dd";
    NSDate * mixDate=[formatter dateFromString:@"1949-01-01"];
//    formatter.dateFormat=@"YYYY-MM";
//    NSDate * mixDate=[formatter dateFromString:@"1949-01"];

    if (self.updateTime && ![self.updateTime isEqualToString:@""]) {
        NSDate *date = [formatter dateFromString:[self.updateTime substringWithRange:NSMakeRange(0, 10)]];
        maxDate =[greg dateByAddingComponents:offsetComponents toDate:date options:0];
    }
    
    self.datePicker.minimumDate=mixDate;
    self.datePicker.maximumDate=localDate;
    
    [self dateChanged:self.datePicker];
}
-(void)dateChanged:(UIDatePicker*)picker{
    
    UIDatePicker * control=(UIDatePicker *)picker;
    NSDate * date=control.date;
    
    NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
    formatter.dateFormat=@"YYYY-MM-dd";
//    formatter.dateFormat=@"YYYY-MM";

    NSString *timestr = [formatter stringFromDate:date];
    self.showStr=timestr;
}
//消除
-(void)cleanBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.showStr=nil;
}
//完成
-(void)sureBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    NSString * brtrhday=self.showStr;
    if (brtrhday) {
        BlockSafeRun(self.returnBlock,brtrhday);
    }
}
-(UIDatePicker *)datePicker{
    if (!_datePicker) {
        _datePicker=[UIDatePicker new];
        _datePicker.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _datePicker.datePickerMode=UIDatePickerModeDate;
        [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _datePicker;
}
@end
